import { Component } from "cc";
import { App } from "../App";
import AbstractController from "../base/AbstractController";
import { LoadMg } from "../tools/LoadMg";
// import { MultiRoom } from "../hall/multiplay/MultiRoom";
import { find } from "cc";
import { MultiRoom } from "../hall/multiplay/MultiRoom";
import { WebSocketTool } from "../net/ws";
import { MultiCommon } from "../hall/multiplay/MultiCommon";
import UtilTool from "../tools/UtilTool";

export default abstract class AbstractComponent extends Component{
    
    public get controller(): AbstractController {
        return this._controller;
    }

    public set controller(value: AbstractController) {
        this._controller = value;
    }
    
    // 退出游戏到大厅（统一走这里）
    public backToHall()
    {
        this.clearScene()
        if (App.fightMode == App.fightMode_1) {
            App.isToHallFromMode1 = true
        }
        
        App.Scene.goHome("home");
    }

    // 返回普通场专业场选择界面（统一走这里）
    public backToMultiSelect()
    {
        this.clearScene()
        App.Scene.go("multiselect")
    }

    /**
     * 退出游戏到多人房间场景（统一走这里）
     * @param roomType 
     * @param fromGameData 如果是从游戏里退出，并且携带了加入房间的数据，就传这个参数
     * formaGameData: {joinData, exitType}
     */
    public backToMultiRoom(roomType=App.Model.gameManage.roomType, fromGameData=null)
    {
        let isFromGame = App.Scene.curScene == "game3" ||  App.Scene.curScene == "game4"

        this.clearScene()
        App.Scene.go("multiroom", null, (err, scene) => {
            if(err) {
                UtilTool.printLog(err);
                return;
            }

            // TODO: 会引起交叉引用报错，先注释掉
            let multiRoom = find('Canvas/MultiRoom', scene).getComponent("MultiRoom")
            // @ts-ignore
            multiRoom.init(roomType);

            // 显示房间加入信息界面
            if(fromGameData) {
                let joinData = fromGameData.joinData
                let exitType = fromGameData.exitType
                MultiCommon.showRoomJoinLayer(joinData, roomType, isFromGame, exitType)
            }

        });
    }

    // 返回血流到底场次选择界面（统一走这里）
    public backToBloodSelect(bloodLevel = null)
    {
        this.clearScene()
        App.Scene.go("bloodselect", null, (err, scene) => {
            if(err) {
                UtilTool.printLog(err);
                return;
            }

            if (bloodLevel!=null) {
                // TODO: 会引起交叉引用报错，先注释掉
                let bloodSelect = find('Canvas/contentNode', scene).getComponent("BloodSelect")
                // @ts-ignore
                bloodSelect.autoMatch(bloodLevel)
            }
        });
    }

    // 退出游戏到登录界面（统一走这里）
    public backToLogin()
    {
        this.clearScene()
        WebSocketTool.instance.activeClose() // 关闭ws
        App.Scene.go("login");
    }

    // 清理场景
    clearScene() {
        LoadMg.showLoad({type:1})
        App.PoolManager.clear()
        App.Model.gameModel.clear()
        App.Model.gameModel3.clear()
        App.Model.gameManage.clear()
        App.Model.clear()
    }

    // 关闭
    public close() {
        this.node.destroy()
    }

    // 点击关闭
    public clickClose() {
        this.playClickAudio()
        this.node.destroy();
    }

    // 播放点击音效
    public playClickAudio(clickId=0) {
        App.Audio.playOneShot("click")
    }

    private _controller: AbstractController;
    // private _model: AbstractModel;
}
